--赛程
local SuperRankAvatarPanel = app.mvc.createPanelClass(...)
local SuperRankAvatarHeadRender = import("..renders.SuperRankAvatarHeadRender")


--data =
function SuperRankAvatarPanel.createWithData(data, node)
    local sprite = SuperRankAvatarPanel.extend(node)
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end

function SuperRankAvatarPanel.create(node)
    return SuperRankAvatarPanel.createWithData(nil, node)
end

function SuperRankAvatarPanel:init(data)
    self.super.init(self, data)

    self._headRenders = {}
    for i = 1, 3 do 
        local headRender = SuperRankAvatarHeadRender.create()
        self._ui["Panel/AvatarPanel"..i]:addChild(headRender)
        self._headRenders[i] = headRender
        headRender:setVisible(false)
    end
    self._ui["Panel/AvatarPanel1"]:addClickEventListener(function()
        if self._data then
            local avatar 
            if self._data.page == 1 then
                avatar = self._data.avatar2
            else
                avatar = self._data.avatar1
            end
            
            if avatar and self._headRenders[1]:isVisible() then
                local headPos = self._headRenders[1]:convertToWorldSpace(cc.p(0, 0))
                UIHandler:showHeadMenu({online=true, type = Enums.MenuType.observeAndGuild, guildInfo = avatar.guildInfo, pos = cc.p(headPos.x, headPos.y - 40)})
            end
        end
        
    end)
    
    self._ui["Panel/AvatarPanel2"]:addClickEventListener(function()
        if self._data then
            local avatar 
            if self._data.page == 1 then
                avatar = self._data.avatar1
            else
                avatar = self._data.avatar2
            end
            if avatar and self._headRenders[2]:isVisible() then
                local headPos = self._headRenders[2]:convertToWorldSpace(cc.p(0, 0))
                UIHandler:showHeadMenu({online=true, type = Enums.MenuType.observeAndGuild, guildInfo = avatar.guildInfo, pos = cc.p(headPos.x, headPos.y - 40)})
            end
        end
    end)

    self._ui["Panel/AvatarPanel3"]:addClickEventListener(function()
        if self._data then
            local avatar = self._data.avatar3
            if avatar and self._headRenders[3]:isVisible() then
                local headPos = self._headRenders[3]:convertToWorldSpace(cc.p(0, 0))
                UIHandler:showHeadMenu({online=true, type = Enums.MenuType.observeAndGuild, guildInfo = avatar.guildInfo, pos = cc.p(headPos.x, headPos.y - 40)})
            end
        end
    end)
end

function SuperRankAvatarPanel:refresh()
    if not self._data then
        return
    end
    local data = self._data
    local title = string.format(L("CSGuildWarAwardTitle"), (data.page -1) * 3 + 1, data.page * 3)
    self._ui["Panel/tlt"]:setString(title)
    if data.page == 1 then
        self._ui["Panel/Panel_bg/bom_1"]:setVisible(true)
        self._ui["Panel/Panel_bg/bom_2"]:setVisible(false)
        self._ui["Panel/AvatarPanel1"]:setPositionY(110)
        self._ui["Panel/AvatarPanel2"]:setPositionY(131)
        self._ui["Panel/AvatarPanel3"]:setPositionY(100)
        self:_loadAvatar(data.avatar1, self._ui["Panel/AvatarPanel2/Avatar"], 2)
        self:_loadAvatar(data.avatar2, self._ui["Panel/AvatarPanel1/Avatar"], 1)
        self:_loadAvatar(data.avatar3, self._ui["Panel/AvatarPanel3/Avatar"], 3)

        self:_setRank(self._ui["Panel/AvatarPanel1/num"], data.avatar2)
        self:_setRank(self._ui["Panel/AvatarPanel2/num"], data.avatar1)
        self:_setRank(self._ui["Panel/AvatarPanel3/num"], data.avatar3)

    else
        self._ui["Panel/Panel_bg/bom_1"]:setVisible(false)
        self._ui["Panel/Panel_bg/bom_2"]:setVisible(true)
        self._ui["Panel/AvatarPanel1"]:setPositionY(90)
        self._ui["Panel/AvatarPanel2"]:setPositionY(90)
        self._ui["Panel/AvatarPanel3"]:setPositionY(90)

        self:_loadAvatar(data.avatar1, self._ui["Panel/AvatarPanel1/Avatar"], 1)
        self:_loadAvatar(data.avatar2, self._ui["Panel/AvatarPanel2/Avatar"], 2)
        self:_loadAvatar(data.avatar3, self._ui["Panel/AvatarPanel3/Avatar"], 3)

        
        self:_setRank(self._ui["Panel/AvatarPanel1/num"], data.avatar1)
        self:_setRank(self._ui["Panel/AvatarPanel2/num"], data.avatar2)
        self:_setRank(self._ui["Panel/AvatarPanel3/num"], data.avatar3)
    end
end


function SuperRankAvatarPanel:_setRank(parentNode, avatarData)
    if not avatarData then
        parentNode:setVisible(flase)
    else
        local rank = avatarData.rank
        parentNode:setVisible(true)

        local label = parentNode:findChild("data_2/num")
        if label then
            label:setString(string.format(L("rankrewardtitle"), rank))
        end
    end
end


function SuperRankAvatarPanel:_loadAvatar(avatarInfo, parentNode, index)
    if avatarInfo then
        parentNode:removeAllChildren()
        self._headRenders[index]:setVisible(false)
        local baseUserInfo = avatarInfo.baseUserInfo
        local AvatarFactory = app:getClass("AvatarFactory")
        local function onCreateCharacter()
            if tolua.isnull(self) then return end
            parentNode:removeAllChildren()
            local baseUserInfo2 = avatarInfo.baseUserInfo
            if baseUserInfo2 then
                local character = AvatarFactory.createHeroIgnoreMount(baseUserInfo2)
                character:playAni({name = "mapidle"})
                character:setScale(0.8)
                parentNode:removeAllChildren()
                parentNode:addChild(character)
                character:setTag(100)
                self._headRenders[index]:setVisible(true)
                self._headRenders[index]:setData(avatarInfo)
                character:addAttachment(self._headRenders[index],"point",0,0)
            end
        end
        AvatarFactory.preloadHero(baseUserInfo, function()
            onCreateCharacter(baseUserInfo)
        end)
    else
        parentNode:removeAllChildren()
        self._headRenders[index]:setVisible(false)
    end
end


return SuperRankAvatarPanel